package cn.edu.zjx2.service.impl;

import cn.edu.mp.entity.Class;
import cn.edu.mp.entity.Grade;
import cn.edu.mp.entity.Major;
import cn.edu.mp.entity.Student;
import cn.edu.mp.mapper.StudentMapper;
import cn.edu.mp.service.ClassService;
import cn.edu.mp.service.GradeService;
import cn.edu.mp.service.MajorService;
import cn.edu.mp.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClassService classService;
    
    @Autowired
    private GradeService gradeService;
    
    @Autowired
    private MajorService majorService;

    @Override
    public List<Student> list() {
        // 获取所有学生
        List<Student> students = super.list();
        if (students.isEmpty()) {
            return students;
        }

        // 获取所有班级
        List<Class> classes = classService.list();
        Map<Long, String> classMap = classes.stream()
                .collect(Collectors.toMap(Class::getId, Class::getClassName));

        // 获取所有年级
        List<Grade> grades = gradeService.list();
        Map<Long, String> gradeMap = grades.stream()
                .collect(Collectors.toMap(Grade::getId, Grade::getGradeName));

        // 获取所有专业
        List<Major> majors = majorService.list();
        Map<Long, String> majorMap = majors.stream()
                .collect(Collectors.toMap(Major::getId, Major::getMajorName));

        // 设置关联信息
        students.forEach(student -> {
            student.setClassName(classMap.get(student.getClassId()));
            student.setGradeName(gradeMap.get(student.getGradeId()));
            student.setMajorName(majorMap.get(student.getMajorId()));
        });

        return students;
    }
} 